//
//  Seven_httpKeyData.m
//  K-Music
//
//  Created by CHUN on 16/2/20.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "Seven_httpKeyData.h"

#import "QiniuSDK.h"

@implementation Seven_httpKeyData

//7牛  token
+ (void)requestSeven_token:(NSData *)paramData sucess:(void (^)( id responseObjectUrl))SevenSucess
                   failure:(void (^)( id Url))SevenFailure {
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    NSString*infoStr11=[NSString stringWithFormat:@"/api/qiniu/get-upload-token?page=0&size=20&sort=createdDate,DESC"];
    
    NSString *intactURL = [MyBaseUrl stringByAppendingString:infoStr11];
    
    NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
    
    //    创建请求
    manager.requestSerializer =[AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json"
                     forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                     forHTTPHeaderField:@"Authorization"];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:intactURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        NSLog(@"成功GET --> 7牛 - %@", dic);
        
        NSDictionary * body = dic[@"body"];
        
        [User_information conversion].domainName_seven = [body objectForKey:@"domainName"];
        
        [User_information conversion].key_seven = [body objectForKey:@"key"];
        
        [User_information conversion].token_seven = [body objectForKey:@"token"];
        
        NSString *  Seven_URL_String =[NSString stringWithFormat:@"%@%@",[User_information conversion].domainName_seven,[User_information conversion].key_seven];
        
        NSString * token = [User_information conversion].token_seven;
        QNUploadManager *upManager = [[QNUploadManager alloc] init];
        //        UIImage *image=[UIImage imageNamed:@"back.png"];
        [upManager putData:paramData key:[User_information conversion].key_seven token:token
                  complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
                      
                      NSLog(@"%@", info);
                      
                      NSLog(@"%@", resp);
                      
                      NSLog(@"上传成功--> %@",Seven_URL_String);
                   
                      SevenSucess(Seven_URL_String);
                      
                  } option:nil];
        
    
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        SevenFailure(nil);
        
        NSLog(@"%@",error);
        
    }];
}



@end
